package cn.zc.controller.admin;

import cn.zc.entity.*;
import cn.zc.mapper.AdminMapper;
import cn.zc.mapper.CourceMapper;
import cn.zc.mapper.StudentMapper;
import cn.zc.result.Result;
import cn.zc.service.AdminService;
import cn.zc.service.UserService;
import cn.zc.utils.CourceUtils;
import cn.zc.utils.QiandaoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
//@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.PATCH, RequestMethod.OPTIONS})
public class adminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CourceMapper courceMapper;
    @Autowired
    private StudentMapper studentMapper;

    @PostMapping("/deleteStudent")
    public Result deleteStudent(@RequestBody Map<String, String> userid) {
        userService.deleteStudent(userid.get("id"));
        return  Result.success();
    }
//    @Transactional
    @PostMapping("/setweek")
    public Result setweek(@RequestBody Map<String, String> nowweek){
        try {
            //将week转换为为int
            String s = nowweek.get("nowweek");
            int weekInt = Integer.parseInt(s);
            adminService.setWeek(weekInt);
            CourceUtils courceUtils = new CourceUtils();
            //清空登录日志
            adminService.clearLoginLog();
            //设置课程的状态是否签到，一个一个人设置
            List<User> userInfo = adminMapper.getUserInfo();
            for (User user : userInfo){
                List<CourceSQL> courceVo = courceMapper.getcourcevoByid(user.getId());
                List<CourceSQL> courceSQLS = CourceUtils.setIsOverlap(courceVo, courceMapper.getWeek());
                //开始一次修改isover
                for (CourceSQL c:courceSQLS){
                    courceMapper.upUserIsover(c);
                }
            }
            //删除所有迟到数据
            studentMapper.deleteAllViolation();
        } catch (NumberFormatException e) {
            return Result.error("修改错误");
        }
        return Result.success(1);
    }

    @GetMapping("/admininfo")
        public Result admininfo(){
        return Result.success(adminService.getAdminInfo());
    }
        @GetMapping("/gettime")
    public Result gettime(){
        List<TimeVo> time = adminService.getTime();
        int nowTime = adminService.getNowTime();
        AdminGetTimeVO adminGetTimeVOBuilder =new AdminGetTimeVO();
        adminGetTimeVOBuilder.setNowWeek(nowTime);
        adminGetTimeVOBuilder.setTime(time);
        return Result.success(adminGetTimeVOBuilder);
    }
    @PostMapping("/deleteadmin")
    public Result deleteadmin(@RequestBody Map<String, String> map) {
        adminService.deleteAdmin(map.get("id"));
        return Result.success("删除成功");
    }
    @PostMapping("/settime")
    public Result settime(@RequestBody TimeVo timeVo) {
        // 从请求参数中获取数组和索引
        return  adminService.setTime(timeVo);
//        System.out.println(timeVo);
//        return Result.success();
    }

    /**
     * 查询违纪信息
     * @return
     */
//    @GetMapping("/overtime")
//    public Result  overtime (int page){
//        List<LateTimeCource> overtime = userService.overtimeAll(page, 10);
//        return Result.success(overtime);
//    }
    @GetMapping("overtimelognum")
    public Result overtimelognum(String search){
        if (search == null || search.isEmpty()){
            search = "";
        }
        int nameOrId = QiandaoUtils.isNameOrId(search);
        int overtimelognum = adminService.overtimelognum(search,nameOrId);
        return Result.success(overtimelognum);
    }
    /**
     * 分页查询违纪信息
     * @return
     */
    @GetMapping("/overtimepage")
    public Result overtimepage(@RequestParam int page, int pageSize,String search) {
        if (search == null || search.isEmpty()){
            search = "";
        }
        int nameOrId = QiandaoUtils.isNameOrId(search);
        List<LateTimeCource> overtime = userService.overtimeAll(page, pageSize,search,nameOrId);
        return Result.success(overtime);
    }
    @PostMapping("/buqian")
    public Result buqian(@RequestBody LateTimeCource buqianvo){
        userService.buqian(buqianvo);
        userService.setviolation(buqianvo);
        return Result.success("补签成功");
    }
    @PostMapping("/editadmin")
    public Result editadmin(@RequestBody User admin){
        if(admin.getName() == null || admin.getName().equals("")){
            return Result.error("用户名不能为空");
        }
        if(admin.getMypassword() == null || admin.getMypassword().equals("")){
            return Result.error("密码不能为空");
        }
        userService.editAdmin(admin);
        return Result.success("修改成功");
    }
    @PostMapping("/addadmin")
    public Result addadmin(@RequestBody User admin){
        try {
            if (admin.getId() == null || admin.getId().equals("")){
                return Result.error("id不能为空");
            }
            if(admin.getName() == null || admin.getName().equals("")){
                return Result.error("用户名不能为空");
            }
            //判断管理员的用户名是否重复
            List<User> userList = adminService.getAdminInfo();
            for (User user : userList) {
                if (user.getName().equals(admin.getName())) {
                    return Result.error("用户名重复，请重新输入");
                }
            }
            if(admin.getMypassword() == null || admin.getMypassword().equals("")){
                return Result.error("密码不能为空");
            }
            userService.addAdmin(admin);
        } catch (Exception e) {
            return Result.error("添加失败,ID重复，请重新输入");
        }
        return Result.success("添加成功");
    }
    public void setShouldPerformTask(){

    }
}
